﻿@model IList<UserGroupsEntity>
	<div class="layui-card">
		<div class="layui-card-header">已经选定的用户</div>
		<div class="layui-card-body">
			<div class="layui-form-item">
				<div class="layui-input-inline" style="width:45%;">
					<input type="text" class="layui-input" id="txtUserNameList" readonly="readonly" />
				</div>
				<div class="layui-input-inline" style="width:auto;">
					<button id="btnOK" type="button" class="layui-btn layuiadmin-btn-list" onclick="layer_close()">确定选中</button>
					<button id="btnDel" type="button" class="layui-btn layuiadmin-btn-list" onclick="Del(1)">删除最后</button>
					<button id="btnDelAll" type="button" class="layui-btn layuiadmin-btn-list" onclick="Del(0)">删除全部</button>
				</div>
			</div>
		</div>
	</div>
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label" style="padding:0px;width:120px;">
						<select id="dropGroups">
							<option value="0">选择会员组</option>
							@foreach (var item in Model)
							{
								<option value="@item.GroupID">@item.GroupName</option>
							}
						</select>
					</label>
					<div class="layui-input-inline" style="margin-right:0px;width:100px;">
						<select id="dropSearchName">
							<option value="UserName">会员名</option>
						</select>
					</div>
					<div class="layui-input-inline" style="margin-right:0px;">
						<input type="text" class="layui-input" id="txtSearchKeyword" name="txtSearchKeyword" placeholder="要搜索的会员名" autocomplete="off" onkeypress="javascript:return DefaultButton(event, 'btnSearch')">
					</div>
				</div>
				<div class="layui-inline">
					<button id="btnSearch" type="button" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnSearch">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
				</div>
				<span class="r">共有数据：<strong id="ctlRecordTotal">0</strong> 条</span>
			</div>
		</div>
		<div class="layui-card-body">
			<div class="nav navbar-nav"><ul id="tableBody" class="cl"></ul></div>
			<div id="grid_paging_part" style="text-align:right;"></div>
			<p class="mt-10"><input class="layui-btn layui-btn-fluid" type="button" value="增加以上所有用户" onclick="AddPageAllUser()"></p>
			<input type="hidden" id="hidPageAllUser" value="" />
		</div>
	</div>
@section scripts {
	<script type="text/javascript">
		var OpenerText = "";
		var RecordTotal;//总记录数
		var limit = 50;//每页数量
		layui.use(['form'], function () {
			var form = layui.form;

			//监听搜索操作
			form.on('submit(btnSearch)', function (data) {
				loadTables(1, limit);
				return false;
			});
		});
		$(function () {
			OpenerText = getUrlParam("OpenerText");
			document.getElementById('txtUserNameList').value = parent.document.getElementById(OpenerText).value;
			loadTables(1, limit);
			showLayPage();
		});
		function loadTables(PageNum, PageSize) {
			$("#tableBody").html("");
			var groupID = $("#dropGroups").val();
			var SearchName = $("#dropSearchName").val();
			var SearchKeyword = $("#txtSearchKeyword").val();
			var parm = "PageNum=" + PageNum + "&PageSize=" + PageSize
				+ "&groupID=" + encodeURIComponent(groupID)
				+ "&SearchName=" + encodeURIComponent(SearchName)
				+ "&SearchKeyword=" + encodeURIComponent(SearchKeyword);
			$.ajax({
				type: "GET",
				url: "/Admin/User/GetUserList?" + parm + "&_t=" + new Date().getTime(),
				async: false,
				contentType: "application/x-www-form-urlencoded;charset=UTF-8",
				success: function (data) {
					var PageAllUser = "";
					$.each(data.RowList, function (i, item) {
						var tr = "<li style='width:25%;'>";
						tr += "<a title='" + item.UserName + "' href=\"javascript:;\" style=\"text-decoration:none;color:black;\" onclick=\"AddItem('" + item.UserName + "')\">" + item.UserName + "</a>";
						tr += "</li>";
						$("#tableBody").append(tr);
						PageAllUser = PageAllUser + item.UserName + ",";
					})
					$("#ctlRecordTotal").html(data.RecordTotal);
					if (PageAllUser.length > 0) {
						PageAllUser = PageAllUser.substring(0, PageAllUser.length - 1);
					}
					$("#hidPageAllUser").val(PageAllUser);
					RecordTotal = data.RecordTotal;
				},
				error: function (data, status, e) {
					layer.msg('得到数据失败!');
				}
			})
		};
		function showLayPage() {
			layui.use(['laypage'], function () {
				var laypage = layui.laypage;
				laypage.render({
					elem: 'grid_paging_part'
					, count: RecordTotal //数据总数，从服务端得到
					, limit: limit
					, jump: function (obj, first) {
						//首次不执行
						if (!first) {
							loadTables(obj.curr, obj.limit);
						}
					}
				});
			});
		};
		function AddItem(obj) {
			if (obj == "") { return false; }
			if (parent.document.getElementById(OpenerText).value == "") {
				parent.document.getElementById(OpenerText).value = obj;
				document.getElementById('txtUserNameList').value = parent.document.getElementById(OpenerText).value;
				return false;
			}
			var singleUserName = obj.split(",");
			var ignoreUserName = "";
			for (i = 0; i < singleUserName.length; i++) {
				if (checkUserName(parent.document.getElementById(OpenerText).value, singleUserName[i])) {
					ignoreUserName = ignoreUserName + singleUserName[i] + " ";
				}
				else {
					parent.document.getElementById(OpenerText).value = parent.document.getElementById(OpenerText).value + "," + singleUserName[i];
					document.getElementById('txtUserNameList').value = parent.document.getElementById(OpenerText).value;
				}
			}
		}
		function AddPageAllUser() {
			var UserNameList = $("#hidPageAllUser").val();
			AddItem(UserNameList);
			layer_close();
		}
		function checkUserName(UserNamelist, thisUserName) {
			if (UserNamelist == thisUserName) {
				return true;
			}
			else {
				var s = UserNamelist.split(",");
				for (j = 0; j < s.length; j++) {
					if (s[j] == thisUserName)
						return true;
				}
				return false;
			}
		}
		function Del(num) {
			if (num == 0 || parent.document.getElementById(OpenerText).value == ""
				|| parent.document.getElementById(OpenerText).value == ",") {
				parent.document.getElementById(OpenerText).value = "";
				document.getElementById('txtUserNameList').value = "";
				return false;
			}

			var strDel = parent.document.getElementById(OpenerText).value;
			var s = strDel.split(",");
			parent.document.getElementById(OpenerText).value = strDel.substring(0, strDel.length - s[s.length - 1].length - 1);
			document.getElementById('txtUserNameList').value = parent.document.getElementById(OpenerText).value;
		}
	</script>
}